package com.beg.cant.controller;

import com.beg.cant.comment.result.Result;
import com.beg.cant.dto.PageRequest;
import com.beg.cant.dto.PageResponse;
import com.beg.cant.dto.WithdrawDTO;
import com.beg.cant.dto.WithdrawQueryRequest;
import com.beg.cant.entity.Withdraw;
import com.beg.cant.service.WithdrawService;
import com.beg.cant.vo.WithdrawVO;
import com.beg.cant.vo.WithdrawStatisticsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/api/withdraw")
@RequiredArgsConstructor
@Tag(name = "提现管理", description = "提现记录的增删改查接口")
public class WithdrawController {
    private final WithdrawService withdrawService;

    @GetMapping("/withdraw/{id}")
    @Operation(summary = "提现记录", description = "用户获取提现记录")
    public Result<List<Withdraw>> getUserWithdraw(@PathVariable Long id) {
        return Result.success(withdrawService.getUserById(id));
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询提现记录", description = "支持按状态和用户ID筛选的分页查询")
    public Result<PageResponse<WithdrawVO>> getWithdrawList(PageRequest request) {
        return Result.success(withdrawService.getWithdrawList(request));
    }

    @PostMapping("/withdraw")
    @Operation(summary = "提现", description = "用户发起提现")
    public Result<String> getUserWithdraw(@RequestBody WithdrawDTO withdrawDTO) {
        withdrawService.setWithdraw(withdrawDTO);
        return Result.success();
    }

    @PostMapping("/finish")
    @Operation(summary = "审核提现",description = "管理审核提现记录")
    public Result<String> finishWithdraw(@RequestBody WithdrawDTO code) throws UnsupportedEncodingException {
        log.info("审核通过提现{}",code);
        withdrawService.finishWithdraw(code.getCode());
        return Result.success("审核通过");
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取提现统计信息", description = "统计今日提现总额、待审核金额、本月提现总额和累计提现总额")
    public Result<WithdrawStatisticsVO> getWithdrawStatistics() {
        return Result.success(withdrawService.getWithdrawStatistics());
    }
}
